{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "8ce6a75d",
   "metadata": {},
   "source": [
    "# Getting Started with Data Sources\n",
    "\n",
    "Welcome to **PyBroker**! The best place to start is to learn about [DataSources](https://www.pybroker.com/en/latest/reference/pybroker.data.html#pybroker.data.DataSource).  A ```DataSource``` is a class that can fetch data from external sources, which you can then use to backtest your trading strategies.\n",
    "\n",
    "## Yahoo Finance\n",
    "\n",
    "One of the built-in ```DataSources``` in **PyBroker** is  [Yahoo Finance](https://finance.yahoo.com). To use it, you can import [YFinance](https://www.pybroker.com/en/latest/reference/pybroker.data.html#pybroker.data.YFinance):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f034d992",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-08-15T11:35:36.017796Z",
     "start_time": "2023-08-15T11:35:32.119216400Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Loading bar data...\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[*********************100%%**********************]  2 of 2 completed"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Loaded bar data: 0:00:00 \n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>date</th>\n",
       "      <th>symbol</th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>close</th>\n",
       "      <th>volume</th>\n",
       "      <th>adj_close</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2021-03-01</td>\n",
       "      <td>AAPL</td>\n",
       "      <td>123.750000</td>\n",
       "      <td>127.930000</td>\n",
       "      <td>122.790001</td>\n",
       "      <td>127.790001</td>\n",
       "      <td>116307900</td>\n",
       "      <td>125.599655</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2021-03-01</td>\n",
       "      <td>MSFT</td>\n",
       "      <td>235.899994</td>\n",
       "      <td>237.470001</td>\n",
       "      <td>233.149994</td>\n",
       "      <td>236.940002</td>\n",
       "      <td>25324000</td>\n",
       "      <td>230.847702</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2021-03-02</td>\n",
       "      <td>AAPL</td>\n",
       "      <td>128.410004</td>\n",
       "      <td>128.720001</td>\n",
       "      <td>125.010002</td>\n",
       "      <td>125.120003</td>\n",
       "      <td>102260900</td>\n",
       "      <td>122.975403</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2021-03-02</td>\n",
       "      <td>MSFT</td>\n",
       "      <td>237.009995</td>\n",
       "      <td>237.300003</td>\n",
       "      <td>233.449997</td>\n",
       "      <td>233.869995</td>\n",
       "      <td>22812500</td>\n",
       "      <td>227.856628</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2021-03-03</td>\n",
       "      <td>AAPL</td>\n",
       "      <td>124.809998</td>\n",
       "      <td>125.709999</td>\n",
       "      <td>121.839996</td>\n",
       "      <td>122.059998</td>\n",
       "      <td>112966300</td>\n",
       "      <td>119.967857</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>501</th>\n",
       "      <td>2022-02-24</td>\n",
       "      <td>MSFT</td>\n",
       "      <td>272.510010</td>\n",
       "      <td>295.160004</td>\n",
       "      <td>271.519989</td>\n",
       "      <td>294.589996</td>\n",
       "      <td>56989700</td>\n",
       "      <td>289.353271</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>502</th>\n",
       "      <td>2022-02-25</td>\n",
       "      <td>AAPL</td>\n",
       "      <td>163.839996</td>\n",
       "      <td>165.119995</td>\n",
       "      <td>160.869995</td>\n",
       "      <td>164.850006</td>\n",
       "      <td>91974200</td>\n",
       "      <td>162.987427</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>503</th>\n",
       "      <td>2022-02-25</td>\n",
       "      <td>MSFT</td>\n",
       "      <td>295.140015</td>\n",
       "      <td>297.630005</td>\n",
       "      <td>291.649994</td>\n",
       "      <td>297.309998</td>\n",
       "      <td>32546700</td>\n",
       "      <td>292.024872</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>504</th>\n",
       "      <td>2022-02-28</td>\n",
       "      <td>AAPL</td>\n",
       "      <td>163.059998</td>\n",
       "      <td>165.419998</td>\n",
       "      <td>162.429993</td>\n",
       "      <td>165.119995</td>\n",
       "      <td>95056600</td>\n",
       "      <td>163.254364</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>505</th>\n",
       "      <td>2022-02-28</td>\n",
       "      <td>MSFT</td>\n",
       "      <td>294.309998</td>\n",
       "      <td>299.140015</td>\n",
       "      <td>293.000000</td>\n",
       "      <td>298.790009</td>\n",
       "      <td>34627500</td>\n",
       "      <td>293.478607</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>506 rows × 8 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "          date symbol        open        high         low       close  \\\n",
       "0   2021-03-01   AAPL  123.750000  127.930000  122.790001  127.790001   \n",
       "1   2021-03-01   MSFT  235.899994  237.470001  233.149994  236.940002   \n",
       "2   2021-03-02   AAPL  128.410004  128.720001  125.010002  125.120003   \n",
       "3   2021-03-02   MSFT  237.009995  237.300003  233.449997  233.869995   \n",
       "4   2021-03-03   AAPL  124.809998  125.709999  121.839996  122.059998   \n",
       "..         ...    ...         ...         ...         ...         ...   \n",
       "501 2022-02-24   MSFT  272.510010  295.160004  271.519989  294.589996   \n",
       "502 2022-02-25   AAPL  163.839996  165.119995  160.869995  164.850006   \n",
       "503 2022-02-25   MSFT  295.140015  297.630005  291.649994  297.309998   \n",
       "504 2022-02-28   AAPL  163.059998  165.419998  162.429993  165.119995   \n",
       "505 2022-02-28   MSFT  294.309998  299.140015  293.000000  298.790009   \n",
       "\n",
       "        volume   adj_close  \n",
       "0    116307900  125.599655  \n",
       "1     25324000  230.847702  \n",
       "2    102260900  122.975403  \n",
       "3     22812500  227.856628  \n",
       "4    112966300  119.967857  \n",
       "..         ...         ...  \n",
       "501   56989700  289.353271  \n",
       "502   91974200  162.987427  \n",
       "503   32546700  292.024872  \n",
       "504   95056600  163.254364  \n",
       "505   34627500  293.478607  \n",
       "\n",
       "[506 rows x 8 columns]"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from pybroker import YFinance\n",
    "\n",
    "yfinance = YFinance()\n",
    "df = yfinance.query(['AAPL', 'MSFT'], start_date='3/1/2021', end_date='3/1/2022')\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "63f2031e",
   "metadata": {},
   "source": [
    "The above code queries data for AAPL and MSFT stocks, and returns a [Pandas DataFrame](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.html) with the results.\n",
    "\n",
    "## Caching Data\n",
    "\n",
    "If you want to speed up your data retrieval, you can cache your queries using **PyBroker**'s caching system. You can enable caching by calling  [pybroker.enable_data_source_cache('name')](https://www.pybroker.com/en/latest/reference/pybroker.cache.html#pybroker.cache.enable_data_source_cache) where ```name``` is the name of the cache you want to use:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "6b718f07",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-08-15T11:35:36.102496400Z",
     "start_time": "2023-08-15T11:35:36.014759400Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<diskcache.core.Cache at 0x7f3884390d60>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pybroker\n",
    "\n",
    "pybroker.enable_data_source_cache('yfinance')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c35fbc8e",
   "metadata": {},
   "source": [
    "The next call to [query](https://www.pybroker.com/en/latest/reference/pybroker.data.html#pybroker.data.DataSource.query) will cache the returned data to disk. Each unique combination of ticker symbol and date range will be cached separately:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "7e07adbe",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-08-15T11:35:37.340726600Z",
     "start_time": "2023-08-15T11:35:36.042740100Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Loading bar data...\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[*********************100%%**********************]  2 of 2 completed"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Loaded bar data: 0:00:00 \n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>date</th>\n",
       "      <th>symbol</th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>close</th>\n",
       "      <th>volume</th>\n",
       "      <th>adj_close</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2021-03-01</td>\n",
       "      <td>IBM</td>\n",
       "      <td>115.057358</td>\n",
       "      <td>116.940727</td>\n",
       "      <td>114.588913</td>\n",
       "      <td>115.430206</td>\n",
       "      <td>5977367</td>\n",
       "      <td>100.173241</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2021-03-01</td>\n",
       "      <td>TSLA</td>\n",
       "      <td>230.036667</td>\n",
       "      <td>239.666672</td>\n",
       "      <td>228.350006</td>\n",
       "      <td>239.476669</td>\n",
       "      <td>81408600</td>\n",
       "      <td>239.476669</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2021-03-02</td>\n",
       "      <td>IBM</td>\n",
       "      <td>115.430206</td>\n",
       "      <td>116.539200</td>\n",
       "      <td>114.971321</td>\n",
       "      <td>115.038239</td>\n",
       "      <td>4732418</td>\n",
       "      <td>99.833076</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2021-03-02</td>\n",
       "      <td>TSLA</td>\n",
       "      <td>239.426666</td>\n",
       "      <td>240.369995</td>\n",
       "      <td>228.333328</td>\n",
       "      <td>228.813339</td>\n",
       "      <td>71196600</td>\n",
       "      <td>228.813339</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2021-03-03</td>\n",
       "      <td>IBM</td>\n",
       "      <td>115.200768</td>\n",
       "      <td>117.237091</td>\n",
       "      <td>114.703636</td>\n",
       "      <td>116.978966</td>\n",
       "      <td>7744898</td>\n",
       "      <td>101.517288</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>501</th>\n",
       "      <td>2022-02-24</td>\n",
       "      <td>TSLA</td>\n",
       "      <td>233.463333</td>\n",
       "      <td>267.493347</td>\n",
       "      <td>233.333328</td>\n",
       "      <td>266.923340</td>\n",
       "      <td>135322200</td>\n",
       "      <td>266.923340</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>502</th>\n",
       "      <td>2022-02-25</td>\n",
       "      <td>IBM</td>\n",
       "      <td>122.050003</td>\n",
       "      <td>124.260002</td>\n",
       "      <td>121.449997</td>\n",
       "      <td>124.180000</td>\n",
       "      <td>4460900</td>\n",
       "      <td>113.041489</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>503</th>\n",
       "      <td>2022-02-25</td>\n",
       "      <td>TSLA</td>\n",
       "      <td>269.743347</td>\n",
       "      <td>273.166656</td>\n",
       "      <td>260.799988</td>\n",
       "      <td>269.956665</td>\n",
       "      <td>76067700</td>\n",
       "      <td>269.956665</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>504</th>\n",
       "      <td>2022-02-28</td>\n",
       "      <td>IBM</td>\n",
       "      <td>122.209999</td>\n",
       "      <td>123.389999</td>\n",
       "      <td>121.040001</td>\n",
       "      <td>122.510002</td>\n",
       "      <td>6757300</td>\n",
       "      <td>111.521271</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>505</th>\n",
       "      <td>2022-02-28</td>\n",
       "      <td>TSLA</td>\n",
       "      <td>271.670013</td>\n",
       "      <td>292.286682</td>\n",
       "      <td>271.570007</td>\n",
       "      <td>290.143341</td>\n",
       "      <td>99006900</td>\n",
       "      <td>290.143341</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>506 rows × 8 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "          date symbol        open        high         low       close  \\\n",
       "0   2021-03-01    IBM  115.057358  116.940727  114.588913  115.430206   \n",
       "1   2021-03-01   TSLA  230.036667  239.666672  228.350006  239.476669   \n",
       "2   2021-03-02    IBM  115.430206  116.539200  114.971321  115.038239   \n",
       "3   2021-03-02   TSLA  239.426666  240.369995  228.333328  228.813339   \n",
       "4   2021-03-03    IBM  115.200768  117.237091  114.703636  116.978966   \n",
       "..         ...    ...         ...         ...         ...         ...   \n",
       "501 2022-02-24   TSLA  233.463333  267.493347  233.333328  266.923340   \n",
       "502 2022-02-25    IBM  122.050003  124.260002  121.449997  124.180000   \n",
       "503 2022-02-25   TSLA  269.743347  273.166656  260.799988  269.956665   \n",
       "504 2022-02-28    IBM  122.209999  123.389999  121.040001  122.510002   \n",
       "505 2022-02-28   TSLA  271.670013  292.286682  271.570007  290.143341   \n",
       "\n",
       "        volume   adj_close  \n",
       "0      5977367  100.173241  \n",
       "1     81408600  239.476669  \n",
       "2      4732418   99.833076  \n",
       "3     71196600  228.813339  \n",
       "4      7744898  101.517288  \n",
       "..         ...         ...  \n",
       "501  135322200  266.923340  \n",
       "502    4460900  113.041489  \n",
       "503   76067700  269.956665  \n",
       "504    6757300  111.521271  \n",
       "505   99006900  290.143341  \n",
       "\n",
       "[506 rows x 8 columns]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "yfinance.query(['TSLA', 'IBM'], '3/1/2021', '3/1/2022')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "03b13620",
   "metadata": {},
   "source": [
    "Calling ```query``` again with the same ticker symbols and date range returns the cached data:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "33569200",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-08-15T11:35:37.381132300Z",
     "start_time": "2023-08-15T11:35:37.338742900Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Loaded cached bar data.\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>date</th>\n",
       "      <th>symbol</th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>close</th>\n",
       "      <th>volume</th>\n",
       "      <th>adj_close</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2021-03-01</td>\n",
       "      <td>IBM</td>\n",
       "      <td>115.057358</td>\n",
       "      <td>116.940727</td>\n",
       "      <td>114.588913</td>\n",
       "      <td>115.430206</td>\n",
       "      <td>5977367</td>\n",
       "      <td>100.173241</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2021-03-02</td>\n",
       "      <td>IBM</td>\n",
       "      <td>115.430206</td>\n",
       "      <td>116.539200</td>\n",
       "      <td>114.971321</td>\n",
       "      <td>115.038239</td>\n",
       "      <td>4732418</td>\n",
       "      <td>99.833076</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2021-03-03</td>\n",
       "      <td>IBM</td>\n",
       "      <td>115.200768</td>\n",
       "      <td>117.237091</td>\n",
       "      <td>114.703636</td>\n",
       "      <td>116.978966</td>\n",
       "      <td>7744898</td>\n",
       "      <td>101.517288</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2021-03-04</td>\n",
       "      <td>IBM</td>\n",
       "      <td>116.634796</td>\n",
       "      <td>117.801147</td>\n",
       "      <td>113.537285</td>\n",
       "      <td>114.827919</td>\n",
       "      <td>8439651</td>\n",
       "      <td>99.650551</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2021-03-05</td>\n",
       "      <td>IBM</td>\n",
       "      <td>115.334610</td>\n",
       "      <td>118.307838</td>\n",
       "      <td>114.961761</td>\n",
       "      <td>117.428299</td>\n",
       "      <td>7268968</td>\n",
       "      <td>101.907227</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>248</th>\n",
       "      <td>2022-02-22</td>\n",
       "      <td>TSLA</td>\n",
       "      <td>278.043335</td>\n",
       "      <td>285.576660</td>\n",
       "      <td>267.033325</td>\n",
       "      <td>273.843323</td>\n",
       "      <td>83288100</td>\n",
       "      <td>273.843323</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>249</th>\n",
       "      <td>2022-02-23</td>\n",
       "      <td>TSLA</td>\n",
       "      <td>276.809998</td>\n",
       "      <td>278.433319</td>\n",
       "      <td>253.520004</td>\n",
       "      <td>254.679993</td>\n",
       "      <td>95256900</td>\n",
       "      <td>254.679993</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>250</th>\n",
       "      <td>2022-02-24</td>\n",
       "      <td>TSLA</td>\n",
       "      <td>233.463333</td>\n",
       "      <td>267.493347</td>\n",
       "      <td>233.333328</td>\n",
       "      <td>266.923340</td>\n",
       "      <td>135322200</td>\n",
       "      <td>266.923340</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>251</th>\n",
       "      <td>2022-02-25</td>\n",
       "      <td>TSLA</td>\n",
       "      <td>269.743347</td>\n",
       "      <td>273.166656</td>\n",
       "      <td>260.799988</td>\n",
       "      <td>269.956665</td>\n",
       "      <td>76067700</td>\n",
       "      <td>269.956665</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>252</th>\n",
       "      <td>2022-02-28</td>\n",
       "      <td>TSLA</td>\n",
       "      <td>271.670013</td>\n",
       "      <td>292.286682</td>\n",
       "      <td>271.570007</td>\n",
       "      <td>290.143341</td>\n",
       "      <td>99006900</td>\n",
       "      <td>290.143341</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>506 rows × 8 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "          date symbol        open        high         low       close  \\\n",
       "0   2021-03-01    IBM  115.057358  116.940727  114.588913  115.430206   \n",
       "1   2021-03-02    IBM  115.430206  116.539200  114.971321  115.038239   \n",
       "2   2021-03-03    IBM  115.200768  117.237091  114.703636  116.978966   \n",
       "3   2021-03-04    IBM  116.634796  117.801147  113.537285  114.827919   \n",
       "4   2021-03-05    IBM  115.334610  118.307838  114.961761  117.428299   \n",
       "..         ...    ...         ...         ...         ...         ...   \n",
       "248 2022-02-22   TSLA  278.043335  285.576660  267.033325  273.843323   \n",
       "249 2022-02-23   TSLA  276.809998  278.433319  253.520004  254.679993   \n",
       "250 2022-02-24   TSLA  233.463333  267.493347  233.333328  266.923340   \n",
       "251 2022-02-25   TSLA  269.743347  273.166656  260.799988  269.956665   \n",
       "252 2022-02-28   TSLA  271.670013  292.286682  271.570007  290.143341   \n",
       "\n",
       "        volume   adj_close  \n",
       "0      5977367  100.173241  \n",
       "1      4732418   99.833076  \n",
       "2      7744898  101.517288  \n",
       "3      8439651   99.650551  \n",
       "4      7268968  101.907227  \n",
       "..         ...         ...  \n",
       "248   83288100  273.843323  \n",
       "249   95256900  254.679993  \n",
       "250  135322200  266.923340  \n",
       "251   76067700  269.956665  \n",
       "252   99006900  290.143341  \n",
       "\n",
       "[506 rows x 8 columns]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = yfinance.query(['TSLA', 'IBM'], '3/1/2021', '3/1/2022')\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "448ea556",
   "metadata": {},
   "source": [
    "You can clear your cache using [pybroker.clear_data_source_cache](https://www.pybroker.com/en/latest/reference/pybroker.cache.html#pybroker.cache.clear_data_source_cache):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "88641432",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-08-15T11:35:37.412747300Z",
     "start_time": "2023-08-15T11:35:37.352743700Z"
    }
   },
   "outputs": [],
   "source": [
    "pybroker.clear_data_source_cache()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "16a02ffe",
   "metadata": {},
   "source": [
    "Or disable caching altogether using [pybroker.disable_data_source_cache](https://www.pybroker.com/en/latest/reference/pybroker.cache.html#pybroker.cache.disable_data_source_cache):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "069afbdd",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-08-15T11:35:37.412747300Z",
     "start_time": "2023-08-15T11:35:37.357876Z"
    }
   },
   "outputs": [],
   "source": [
    "pybroker.disable_data_source_cache()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3ddd46f1",
   "metadata": {},
   "source": [
    "Note that these calls should be made after first calling [pybroker.enable_data_source_cache](https://www.pybroker.com/en/latest/reference/pybroker.cache.html#pybroker.cache.enable_data_source_cache)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "223c7148",
   "metadata": {},
   "source": [
    "## Alpaca\n",
    "\n",
    "**PyBroker** also includes an [Alpaca](https://alpaca.markets/) ```DataSource``` for fetching stock data. To use it, you can import [Alpaca](https://www.pybroker.com/en/latest/reference/pybroker.data.html#pybroker.data.Alpaca) and provide your API key and secret: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "3059a94d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-08-15T11:35:37.475437700Z",
     "start_time": "2023-08-15T11:35:37.361448900Z"
    }
   },
   "outputs": [],
   "source": [
    "from pybroker import Alpaca\n",
    "import os\n",
    "\n",
    "alpaca = Alpaca(os.environ['ALPACA_API_KEY'], os.environ['ALPACA_API_SECRET'])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e8eb5d95",
   "metadata": {},
   "source": [
    "You can query ```Alpaca``` for stock data using the same syntax as with Yahoo Finance, but Alpaca also supports querying data by different timeframes. For example, to query 1 minute data:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "0f74ea85",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-08-15T11:36:23.802697500Z",
     "start_time": "2023-08-15T11:35:37.367308500Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Loading bar data...\n",
      "Loaded bar data: 0:00:05 \n",
      "\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>date</th>\n",
       "      <th>symbol</th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>close</th>\n",
       "      <th>volume</th>\n",
       "      <th>vwap</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2021-03-01 04:00:00-05:00</td>\n",
       "      <td>AAPL</td>\n",
       "      <td>124.30</td>\n",
       "      <td>124.56</td>\n",
       "      <td>124.30</td>\n",
       "      <td>124.50</td>\n",
       "      <td>12267.0</td>\n",
       "      <td>124.433365</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2021-03-01 04:00:00-05:00</td>\n",
       "      <td>MSFT</td>\n",
       "      <td>235.87</td>\n",
       "      <td>236.00</td>\n",
       "      <td>235.87</td>\n",
       "      <td>236.00</td>\n",
       "      <td>1429.0</td>\n",
       "      <td>235.938887</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2021-03-01 04:01:00-05:00</td>\n",
       "      <td>AAPL</td>\n",
       "      <td>124.56</td>\n",
       "      <td>124.60</td>\n",
       "      <td>124.30</td>\n",
       "      <td>124.30</td>\n",
       "      <td>9439.0</td>\n",
       "      <td>124.481323</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2021-03-01 04:01:00-05:00</td>\n",
       "      <td>MSFT</td>\n",
       "      <td>236.17</td>\n",
       "      <td>236.17</td>\n",
       "      <td>236.17</td>\n",
       "      <td>236.17</td>\n",
       "      <td>104.0</td>\n",
       "      <td>236.161538</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2021-03-01 04:02:00-05:00</td>\n",
       "      <td>AAPL</td>\n",
       "      <td>124.00</td>\n",
       "      <td>124.05</td>\n",
       "      <td>123.78</td>\n",
       "      <td>123.78</td>\n",
       "      <td>4834.0</td>\n",
       "      <td>123.935583</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>33340</th>\n",
       "      <td>2021-03-31 19:57:00-04:00</td>\n",
       "      <td>MSFT</td>\n",
       "      <td>237.28</td>\n",
       "      <td>237.28</td>\n",
       "      <td>237.28</td>\n",
       "      <td>237.28</td>\n",
       "      <td>507.0</td>\n",
       "      <td>237.367870</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>33341</th>\n",
       "      <td>2021-03-31 19:58:00-04:00</td>\n",
       "      <td>AAPL</td>\n",
       "      <td>122.36</td>\n",
       "      <td>122.39</td>\n",
       "      <td>122.33</td>\n",
       "      <td>122.39</td>\n",
       "      <td>3403.0</td>\n",
       "      <td>122.360544</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>33342</th>\n",
       "      <td>2021-03-31 19:58:00-04:00</td>\n",
       "      <td>MSFT</td>\n",
       "      <td>237.40</td>\n",
       "      <td>237.40</td>\n",
       "      <td>237.35</td>\n",
       "      <td>237.35</td>\n",
       "      <td>636.0</td>\n",
       "      <td>237.378066</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>33343</th>\n",
       "      <td>2021-03-31 19:59:00-04:00</td>\n",
       "      <td>AAPL</td>\n",
       "      <td>122.39</td>\n",
       "      <td>122.45</td>\n",
       "      <td>122.38</td>\n",
       "      <td>122.45</td>\n",
       "      <td>5560.0</td>\n",
       "      <td>122.402606</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>33344</th>\n",
       "      <td>2021-03-31 19:59:00-04:00</td>\n",
       "      <td>MSFT</td>\n",
       "      <td>237.40</td>\n",
       "      <td>237.53</td>\n",
       "      <td>237.40</td>\n",
       "      <td>237.53</td>\n",
       "      <td>1163.0</td>\n",
       "      <td>237.473801</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>33345 rows × 8 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                           date symbol    open    high     low   close  \\\n",
       "0     2021-03-01 04:00:00-05:00   AAPL  124.30  124.56  124.30  124.50   \n",
       "1     2021-03-01 04:00:00-05:00   MSFT  235.87  236.00  235.87  236.00   \n",
       "2     2021-03-01 04:01:00-05:00   AAPL  124.56  124.60  124.30  124.30   \n",
       "3     2021-03-01 04:01:00-05:00   MSFT  236.17  236.17  236.17  236.17   \n",
       "4     2021-03-01 04:02:00-05:00   AAPL  124.00  124.05  123.78  123.78   \n",
       "...                         ...    ...     ...     ...     ...     ...   \n",
       "33340 2021-03-31 19:57:00-04:00   MSFT  237.28  237.28  237.28  237.28   \n",
       "33341 2021-03-31 19:58:00-04:00   AAPL  122.36  122.39  122.33  122.39   \n",
       "33342 2021-03-31 19:58:00-04:00   MSFT  237.40  237.40  237.35  237.35   \n",
       "33343 2021-03-31 19:59:00-04:00   AAPL  122.39  122.45  122.38  122.45   \n",
       "33344 2021-03-31 19:59:00-04:00   MSFT  237.40  237.53  237.40  237.53   \n",
       "\n",
       "        volume        vwap  \n",
       "0      12267.0  124.433365  \n",
       "1       1429.0  235.938887  \n",
       "2       9439.0  124.481323  \n",
       "3        104.0  236.161538  \n",
       "4       4834.0  123.935583  \n",
       "...        ...         ...  \n",
       "33340    507.0  237.367870  \n",
       "33341   3403.0  122.360544  \n",
       "33342    636.0  237.378066  \n",
       "33343   5560.0  122.402606  \n",
       "33344   1163.0  237.473801  \n",
       "\n",
       "[33345 rows x 8 columns]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = alpaca.query(\n",
    "    ['AAPL', 'MSFT'], \n",
    "    start_date='3/1/2021', \n",
    "    end_date='4/1/2021', \n",
    "    timeframe='1m'\n",
    ")\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "05c2b17d",
   "metadata": {},
   "source": [
    "## Alpaca Crypto\n",
    "\n",
    "If you are interested in fetching cryptocurrency data, you can use [AlpacaCrypto](https://www.pybroker.com/en/latest/reference/pybroker.data.html#pybroker.data.AlpacaCrypto). Here's an example of how to use it:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "4f0f8826",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-08-15T11:36:25.621317500Z",
     "start_time": "2023-08-15T11:36:23.800161700Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Loading bar data...\n",
      "Loaded bar data: 0:00:06 \n",
      "\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>symbol</th>\n",
       "      <th>date</th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>close</th>\n",
       "      <th>volume</th>\n",
       "      <th>vwap</th>\n",
       "      <th>trade_count</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>BTC/USD</td>\n",
       "      <td>2021-01-01 01:00:00-05:00</td>\n",
       "      <td>29255.71</td>\n",
       "      <td>29338.25</td>\n",
       "      <td>29153.55</td>\n",
       "      <td>29234.15</td>\n",
       "      <td>42.244289</td>\n",
       "      <td>29237.240312</td>\n",
       "      <td>1243.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>BTC/USD</td>\n",
       "      <td>2021-01-01 02:00:00-05:00</td>\n",
       "      <td>29235.61</td>\n",
       "      <td>29236.95</td>\n",
       "      <td>28905.00</td>\n",
       "      <td>29162.50</td>\n",
       "      <td>34.506038</td>\n",
       "      <td>29078.423185</td>\n",
       "      <td>1070.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>BTC/USD</td>\n",
       "      <td>2021-01-01 03:00:00-05:00</td>\n",
       "      <td>29162.50</td>\n",
       "      <td>29248.52</td>\n",
       "      <td>28948.86</td>\n",
       "      <td>29076.77</td>\n",
       "      <td>27.596804</td>\n",
       "      <td>29091.465155</td>\n",
       "      <td>1110.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>BTC/USD</td>\n",
       "      <td>2021-01-01 04:00:00-05:00</td>\n",
       "      <td>29075.31</td>\n",
       "      <td>29372.32</td>\n",
       "      <td>29058.05</td>\n",
       "      <td>29284.92</td>\n",
       "      <td>20.694200</td>\n",
       "      <td>29248.730924</td>\n",
       "      <td>880.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>BTC/USD</td>\n",
       "      <td>2021-01-01 05:00:00-05:00</td>\n",
       "      <td>29291.54</td>\n",
       "      <td>29400.00</td>\n",
       "      <td>29232.16</td>\n",
       "      <td>29286.63</td>\n",
       "      <td>16.617646</td>\n",
       "      <td>29338.609132</td>\n",
       "      <td>742.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>735</th>\n",
       "      <td>BTC/USD</td>\n",
       "      <td>2021-01-31 15:00:00-05:00</td>\n",
       "      <td>32837.67</td>\n",
       "      <td>32964.87</td>\n",
       "      <td>32528.54</td>\n",
       "      <td>32882.87</td>\n",
       "      <td>40.631122</td>\n",
       "      <td>32818.132855</td>\n",
       "      <td>2197.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>736</th>\n",
       "      <td>BTC/USD</td>\n",
       "      <td>2021-01-31 16:00:00-05:00</td>\n",
       "      <td>32889.01</td>\n",
       "      <td>32935.98</td>\n",
       "      <td>32554.59</td>\n",
       "      <td>32586.68</td>\n",
       "      <td>26.673190</td>\n",
       "      <td>32737.975296</td>\n",
       "      <td>1625.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>737</th>\n",
       "      <td>BTC/USD</td>\n",
       "      <td>2021-01-31 17:00:00-05:00</td>\n",
       "      <td>32599.00</td>\n",
       "      <td>33126.32</td>\n",
       "      <td>32599.00</td>\n",
       "      <td>32998.35</td>\n",
       "      <td>25.422568</td>\n",
       "      <td>32923.438893</td>\n",
       "      <td>1770.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>738</th>\n",
       "      <td>BTC/USD</td>\n",
       "      <td>2021-01-31 18:00:00-05:00</td>\n",
       "      <td>33000.00</td>\n",
       "      <td>33263.94</td>\n",
       "      <td>32957.10</td>\n",
       "      <td>33134.86</td>\n",
       "      <td>31.072017</td>\n",
       "      <td>33147.086803</td>\n",
       "      <td>2203.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>739</th>\n",
       "      <td>BTC/USD</td>\n",
       "      <td>2021-01-31 19:00:00-05:00</td>\n",
       "      <td>33134.03</td>\n",
       "      <td>33134.03</td>\n",
       "      <td>32303.44</td>\n",
       "      <td>32572.03</td>\n",
       "      <td>60.460424</td>\n",
       "      <td>32552.937863</td>\n",
       "      <td>2665.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>740 rows × 9 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      symbol                      date      open      high       low  \\\n",
       "0    BTC/USD 2021-01-01 01:00:00-05:00  29255.71  29338.25  29153.55   \n",
       "1    BTC/USD 2021-01-01 02:00:00-05:00  29235.61  29236.95  28905.00   \n",
       "2    BTC/USD 2021-01-01 03:00:00-05:00  29162.50  29248.52  28948.86   \n",
       "3    BTC/USD 2021-01-01 04:00:00-05:00  29075.31  29372.32  29058.05   \n",
       "4    BTC/USD 2021-01-01 05:00:00-05:00  29291.54  29400.00  29232.16   \n",
       "..       ...                       ...       ...       ...       ...   \n",
       "735  BTC/USD 2021-01-31 15:00:00-05:00  32837.67  32964.87  32528.54   \n",
       "736  BTC/USD 2021-01-31 16:00:00-05:00  32889.01  32935.98  32554.59   \n",
       "737  BTC/USD 2021-01-31 17:00:00-05:00  32599.00  33126.32  32599.00   \n",
       "738  BTC/USD 2021-01-31 18:00:00-05:00  33000.00  33263.94  32957.10   \n",
       "739  BTC/USD 2021-01-31 19:00:00-05:00  33134.03  33134.03  32303.44   \n",
       "\n",
       "        close     volume          vwap  trade_count  \n",
       "0    29234.15  42.244289  29237.240312       1243.0  \n",
       "1    29162.50  34.506038  29078.423185       1070.0  \n",
       "2    29076.77  27.596804  29091.465155       1110.0  \n",
       "3    29284.92  20.694200  29248.730924        880.0  \n",
       "4    29286.63  16.617646  29338.609132        742.0  \n",
       "..        ...        ...           ...          ...  \n",
       "735  32882.87  40.631122  32818.132855       2197.0  \n",
       "736  32586.68  26.673190  32737.975296       1625.0  \n",
       "737  32998.35  25.422568  32923.438893       1770.0  \n",
       "738  33134.86  31.072017  33147.086803       2203.0  \n",
       "739  32572.03  60.460424  32552.937863       2665.0  \n",
       "\n",
       "[740 rows x 9 columns]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from pybroker import AlpacaCrypto\n",
    "\n",
    "crypto = AlpacaCrypto(\n",
    "    os.environ['ALPACA_API_KEY'], \n",
    "    os.environ['ALPACA_API_SECRET']\n",
    ")\n",
    "df = crypto.query('BTC/USD', start_date='1/1/2021', end_date='2/1/2021', timeframe='1h')\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6a308188",
   "metadata": {},
   "source": [
    "In the above example, we're querying for hourly data for the BTC/USD currency pair."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8d7dfaa6",
   "metadata": {},
   "source": [
    "## AKShare\n",
    "\n",
    "**PyBroker** also includes an [AKShare](https://github.com/akfamily/akshare) ```DataSource``` for fetching **Chinese** stock data. AKShare, a widely-used open-source package, is tailored for obtaining financial data, with a focus on the Chinese market. This free tool provides users with access to higher quality data compared to yfinance for the Chinese market. To use it, you can import [AKShare](https://www.pybroker.com/en/latest/reference/pybroker.ext.data.html#pybroker.ext.data.AKShare):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "d66daaee",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-08-15T11:36:26.569270700Z",
     "start_time": "2023-08-15T11:36:25.623249Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Loading bar data...\n",
      "Loaded bar data: 0:00:10 \n",
      "\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>date</th>\n",
       "      <th>symbol</th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>close</th>\n",
       "      <th>volume</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2021-03-01</td>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>21.54</td>\n",
       "      <td>21.68</td>\n",
       "      <td>21.18</td>\n",
       "      <td>21.45</td>\n",
       "      <td>1125387</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2021-03-01</td>\n",
       "      <td>600000.SH</td>\n",
       "      <td>10.59</td>\n",
       "      <td>10.64</td>\n",
       "      <td>10.50</td>\n",
       "      <td>10.58</td>\n",
       "      <td>547461</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2021-03-02</td>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>21.62</td>\n",
       "      <td>22.15</td>\n",
       "      <td>21.26</td>\n",
       "      <td>21.65</td>\n",
       "      <td>1473425</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2021-03-02</td>\n",
       "      <td>600000.SH</td>\n",
       "      <td>10.61</td>\n",
       "      <td>10.70</td>\n",
       "      <td>10.36</td>\n",
       "      <td>10.47</td>\n",
       "      <td>747631</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2021-03-03</td>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>21.58</td>\n",
       "      <td>23.08</td>\n",
       "      <td>21.46</td>\n",
       "      <td>23.01</td>\n",
       "      <td>1919635</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>969</th>\n",
       "      <td>2023-02-27</td>\n",
       "      <td>600000.SH</td>\n",
       "      <td>7.16</td>\n",
       "      <td>7.20</td>\n",
       "      <td>7.16</td>\n",
       "      <td>7.16</td>\n",
       "      <td>158006</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>970</th>\n",
       "      <td>2023-02-28</td>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>13.75</td>\n",
       "      <td>13.85</td>\n",
       "      <td>13.61</td>\n",
       "      <td>13.78</td>\n",
       "      <td>607936</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>971</th>\n",
       "      <td>2023-02-28</td>\n",
       "      <td>600000.SH</td>\n",
       "      <td>7.18</td>\n",
       "      <td>7.20</td>\n",
       "      <td>7.14</td>\n",
       "      <td>7.18</td>\n",
       "      <td>174481</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>972</th>\n",
       "      <td>2023-03-01</td>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>13.80</td>\n",
       "      <td>14.19</td>\n",
       "      <td>13.74</td>\n",
       "      <td>14.17</td>\n",
       "      <td>1223452</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>973</th>\n",
       "      <td>2023-03-01</td>\n",
       "      <td>600000.SH</td>\n",
       "      <td>7.17</td>\n",
       "      <td>7.27</td>\n",
       "      <td>7.17</td>\n",
       "      <td>7.26</td>\n",
       "      <td>256613</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>974 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "          date     symbol   open   high    low  close   volume\n",
       "0   2021-03-01  000001.SZ  21.54  21.68  21.18  21.45  1125387\n",
       "1   2021-03-01  600000.SH  10.59  10.64  10.50  10.58   547461\n",
       "2   2021-03-02  000001.SZ  21.62  22.15  21.26  21.65  1473425\n",
       "3   2021-03-02  600000.SH  10.61  10.70  10.36  10.47   747631\n",
       "4   2021-03-03  000001.SZ  21.58  23.08  21.46  23.01  1919635\n",
       "..         ...        ...    ...    ...    ...    ...      ...\n",
       "969 2023-02-27  600000.SH   7.16   7.20   7.16   7.16   158006\n",
       "970 2023-02-28  000001.SZ  13.75  13.85  13.61  13.78   607936\n",
       "971 2023-02-28  600000.SH   7.18   7.20   7.14   7.18   174481\n",
       "972 2023-03-01  000001.SZ  13.80  14.19  13.74  14.17  1223452\n",
       "973 2023-03-01  600000.SH   7.17   7.27   7.17   7.26   256613\n",
       "\n",
       "[974 rows x 7 columns]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from pybroker.ext.data import AKShare\n",
    "\n",
    "akshare = AKShare()\n",
    "# You can substitute 000001.SZ with 000001, and it will still work!\n",
    "# and you can set start_date as \"20210301\" format\n",
    "# You can also set adjust to 'qfq' or 'hfq' to adjust the data, \n",
    "# and set timeframe to '1d', '1w' to get daily, weekly data\n",
    "df = akshare.query(\n",
    "    symbols=['000001.SZ', '600000.SH'], \n",
    "    start_date='3/1/2021', \n",
    "    end_date='3/1/2023',\n",
    "    adjust=\"\", \n",
    "    timeframe=\"1d\",\n",
    ")\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fa593912",
   "metadata": {},
   "source": [
    "**NOTE**: If the above causes a  ``Native library not available`` error and you still want to use AKShare, then [see this issue for details on how to resolve it](https://github.com/edtechre/pybroker/issues/36#issuecomment-1605910339)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "893620ec",
   "metadata": {},
   "source": [
    "[In the next notebook, we'll take a look at how to use DataSources to backtest a simple trading strategy](https://www.pybroker.com/en/latest/notebooks/2.%20Backtesting%20a%20Strategy.html)."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
